package subject_set.lc200;

/**
 * @author haomin
 * @date 2022/12/25 22:17
 **/
public class Num24 {
    // 递归
    class Solution {
        public ListNode swapPairs(ListNode head) {
            if(head == null || head.next == null) return head;

            ListNode fir = head;
            ListNode se = head.next;
            fir.next = swapPairs(se.next);
            se.next = fir;
            return se;
        }
    }

    class Solution2 {
        public ListNode swapPairs(ListNode head) {
            if(head == null || head.next == null) return head;

            ListNode dummyHead = new ListNode(-1);
            dummyHead.next = head;
            ListNode cur = dummyHead;
            ListNode first = head;
            ListNode second = head.next;
            while(first != null) {
                first.next = second.next;
                second.next = first;
                cur.next= second;

                cur = first;
                first = first.next;
                if(first != null && first.next != null) {
                    second = first.next;
                }else return dummyHead.next;
            }
            return dummyHead.next;
        }
    }
}